package com.atguigu.gulimall.order.interceptor;


import com.alibaba.fastjson.JSON;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimall.order.to.UserInfoTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;




public class OrderInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberResponseVo> toThreadLocal = new ThreadLocal<>();
    @Autowired
    public StringRedisTemplate redisTemplate;

    /***
     * 目标方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if(requestURI.contains("detail/")
                || requestURI.contains("pay/")|| requestURI.contains("confirm/")||requestURI.contains("/order/order/status")){
          return true;
      }
        Cookie[] cookies = request.getCookies();
        String sso_token = request.getParameter("sso_token");
        if(sso_token!= null){
            Cookie cookie = new Cookie("sso_token", sso_token);
            cookie.setMaxAge(100*24*60*60);
            response.addCookie(cookie);
                //从用户中获得用户
                String s = redisTemplate.opsForValue().get("login:users:" + sso_token);
                MemberResponseVo  memberResponseVo = JSON.parseObject(s , MemberResponseVo.class);
                toThreadLocal.set(memberResponseVo);
            return true;
        }

        if (cookies != null && cookies.length > 0) {
            MemberResponseVo  memberResponseVo = null;
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if(name.equals("sso_token")){
                    cookie.setMaxAge(100*24*60*60);
                    //从用户中获得用户
                    String s = redisTemplate.opsForValue().get("login:users:" + cookie.getValue());
                    memberResponseVo = JSON.parseObject(s , MemberResponseVo.class);
                    response.addCookie(cookie);
                    break;
                }
            }
            //目标方法执行之前
            toThreadLocal.set(memberResponseVo);
            return true;
        }
        //去登陆页找token
        response.sendRedirect("http://localhost:20000/login.html?url=" + request.getRequestURL());
        return false;
    }


    /**
     * 业务执行之后，分配临时用户来浏览器保存
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
